﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utils;
using Model;
using Dao;
using System.Data;

namespace Website.uc
{
    public partial class ucSaveItems : System.Web.UI.UserControl
    {
        ItemsDao itemDao = new ItemsDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCategory();
                LoadItemInfo();
            }
        }

        public void LoadItemInfo()
        {
            if (Session["item"] != null)
            {
                DataTable tb = itemDao.getItemById(Session["item"].ToString());
                if (tb.Rows.Count > 0)
                {
                    txtDescription.Text = tb.Rows[0]["ItemDescription"].ToString();
                    txtMidIncrement.Text = tb.Rows[0]["BidIncremenent"].ToString();
                    txtMinumumBid.Text = tb.Rows[0]["Minimumbid"].ToString();
                    DateSelector1.CalendarDate = tb.Rows[0]["BidEndDate"].ToString();
                    txtTitle.Text = tb.Rows[0]["ItemTitle"].ToString();
                    lblId.Text = Session["item"].ToString();
                    ddlStatus.Text = tb.Rows[0]["BidStatus"].ToString();
                    Image1.ImageUrl = tb.Rows[0]["ItemImage"].ToString();
                    
                }

            }
        }

        private void LoadCategory()
        {
            DataTable tb = itemDao.getAllCategory();
            ddlCategory.DataSource = tb;
            ddlCategory.DataTextField = "CateName";
            ddlCategory.DataValueField = "CateId";
            ddlCategory.DataBind();
        }

        private void reset()
        {
            txtDescription.Text = "";
            txtMidIncrement.Text = "";
            txtMinumumBid.Text = "";
            DateSelector1.Text = "";
            txtTitle.Text = "";
            lblId.Text = "";
            lblMessage.Text = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Items items = new Items();
            if (CheckValidate.CheckNull(txtTitle.Text))
            {
                lblMessage.Text = "Enter title !!";
                txtTitle.Focus();
            }

            else if (CheckValidate.CheckNull(txtMidIncrement.Text))
            {
                lblMessage.Text = "Enter MidIncrement !!";
                txtMidIncrement.Focus();
            }

            else if (CheckValidate.CheckNull(txtMinumumBid.Text))
            {
                lblMessage.Text = "Enter MinumumBid !!";
                txtMinumumBid.Focus();
            }

            else if (CheckValidate.CheckNull(DateSelector1.Text))
            {
                lblMessage.Text = "Enter StartDate !!";
                DateSelector1.Focus();
            }
            else
            {
                items.BidStartDate = DateTime.Now;
                items.BidEndDate = Convert.ToDateTime(DateSelector1.CalendarDate);
                items.BidStatus = "New";
                items.CateId = Int32.Parse(ddlCategory.SelectedValue.ToString());
                items.ItemDescription = txtDescription.Text;
                items.ItemTitle = txtTitle.Text;
                items.MidIncremenent = txtMidIncrement.Text;
                items.Minimumbid = txtMinumumBid.Text;
                items.CateId = Int32.Parse(ddlCategory.SelectedValue.ToString());
                items.ItemImage = "~/photo/" + FileUpload1.FileName;
                items.UserId = 1;

                if (lblId.Text.Length > 0)
                {
                    items.ItemId = Int32.Parse(lblId.Text);
                    if (FileUpload1.FileName.Length == 0)
                    {
                        itemDao.updateItemsNotImage(items);
                    }
                    else
                    {
                        itemDao.updateItems(items);
                    }

                }
                else
                {
                    itemDao.insertItems(items);
                    SaveAsImage("photo", FileUpload1);
                }
                reset();
            }
        }

        private void SaveAsImage(string path, FileUpload file)
        {
            try
            {
                string savePath = HttpContext.Current.Server.MapPath(string.Format(@"~/{0}/{1}", path, file.FileName));
                file.SaveAs(savePath);
            }
            catch (Exception ex)
            {
                ex.StackTrace.ToString();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            reset();
        }
    }
}